from php4dvd.model.user import User
from php4dvd.pages.add_film_page import AddFilmPage
from php4dvd.pages.film_details_page import FilmDetailsPage
from php4dvd.pages.home_page import HomePage
from php4dvd.pages.internal_page import InternalPage
from php4dvd.pages.login_page import LoginPage
from php4dvd.pages.user_management_page import UserManagementPage
from php4dvd.pages.user_profile_page import UserProfilePage
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.expected_conditions import *
from selenium.webdriver.support.ui import WebDriverWait


class Application(object):

    def __init__(self, driver, base_url):
        driver.get(base_url)
        self.wait = WebDriverWait(driver, 15)
        self.login_page = LoginPage(driver, base_url)
        self.internal_page = InternalPage(driver, base_url)
        self.user_profile_page = UserProfilePage(driver, base_url)
        self.user_management_page = UserManagementPage(driver, base_url)
        self.add_film_page = AddFilmPage(driver, base_url)
        self.film_details_page = FilmDetailsPage(driver, base_url)
        self.home_page = HomePage(driver, base_url)

    #def go_to_home_page(self):
     #   self.driver.get(self.base_url)

    def logout(self):
        self.internal_page.logout_button.click()
        self.wait.until(alert_is_present()).accept()

    def ensure_logout(self):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            self.logout()

    def login(self, user):
        lp = self.login_page
        lp.is_this_page
        lp.username_field.send_keys(user.username)
        lp.password_field.send_keys(user.password)
        lp.submit_button.click()

    def ensure_login_as(self, user):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            if self.is_logged_in_as(user):
                return
            else:
                self.logout()
        self.login(user)

    def is_logged_in(self):
        return self.internal_page.is_this_page

    def is_not_logged_in(self):
        return self.login_page.is_this_page

    def is_logged_in_as(self, user):
        return self.is_logged_in() \
            and self.get_logged_user().username == user.username

    def add_user(self, user):
        self.internal_page.user_management_link.click()
        ump = self.user_management_page
        ump.is_this_page
        ump.user_form.username_field.send_keys(user.username)
        ump.user_form.email_field.send_keys(user.email)
        ump.user_form.password_field.send_keys(user.password)
        ump.user_form.password1_field.send_keys(user.password)
        ump.user_form.submit_button.click()

    def get_logged_user(self):
        self.internal_page.user_profile_link.click()
        upp = self.user_profile_page
        upp.is_this_page
        return User(username=upp.user_form.username_field.get_attribute("value"),
                    email=upp.user_form.email_field.get_attribute("value"))

    def add_film(self, film):
        #search
       # self.internal_page.add_film_button.click()
        self.internal_page.home_link.click()
        hp = self.home_page
        hp.is_this_page
        hp.add_film_button.click()
        afp = self.add_film_page
        afp.is_this_page
        afp.film_form.imdbid_field.send_keys(film.imdbid)
        afp.film_form.name_field.send_keys(film.name)
        afp.film_form.aka_field.send_keys(film.aka)
        afp.film_form.year_field.send_keys(film.year)
        afp.film_form.duration_field.send_keys(film.duration)
        afp.film_form.rating_field.send_keys(film.rating)
        afp.film_form.format_field.send_keys(film.format)
        afp.film_form.trailer_field.send_keys(film.trailer)
        afp.film_form.notes_field.send_keys(film.notes)
        afp.film_form.taglines_field.send_keys(film.taglines)
        afp.film_form.plotoutline_field.send_keys(film.plotoutline)
        afp.film_form.plots_field.send_keys(film.plots)
        afp.film_form.languages_field.send_keys(film.languages)
        afp.film_form.subtitles_field.send_keys(film.subtitles)
        afp.film_form.audio_field.send_keys(film.audio)
        afp.film_form.video_field.send_keys(film.video)
        afp.film_form.country_field.send_keys(film.country)
        afp.film_form.genres_field.send_keys(film.genres)
        afp.film_form.director_field.send_keys(film.director)
        afp.film_form.writer_field.send_keys(film.writer)
        afp.film_form.producer_field.send_keys(film.producer)
        afp.film_form.music_field.send_keys(film.music)
        afp.film_form.cast_field.send_keys(film.cast)
        afp.save_button.click()

    def is_added_film(self, film):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "img[title=\"Remove\"], form[id='imdbsearchform']")))
        if element.tag_name == "img":
            if self.film_details_check(film):
                return True
            else:
                return False
        else: return False

    def film_details_check(self, film):
        fdp = self.film_details_page
        fdp.is_this_page
        return fdp.head.get_attribute("value") == (film.name + " (" + film.year + ")")

    def is_not_added_film(self, film):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "img[title=\"Remove\"], form[id='imdbsearchform']")))
        if (element.tag_name == "form"):
            return True
        else:
            return False

    def delete_film(self, film):
        self.internal_page.home_link.click()
        hp = self.home_page
        hp.is_this_page
        self.search_film(film)
        elem = self.wait.until(presence_of_element_located((By.XPATH, "//div[@class='title' and text()='" + film.name + "']")))
        elem.click()
        fdp = self.film_details_page
        fdp.is_this_page
        fdp.remove_button.click()
        self.wait.until(alert_is_present()).accept()

    def is_film_deleted(self, film):
        #if cnt_before == cnt_after + 1:
            if self.is_film_not_found(film):
                return True
            else:
                return False
       # else:
       #     return False


    #def count_films(self):
        #ip = self.internal_page
        #ip.is_this_page
        #ip.home_link.click()
        #hp = self.home_page
        #hp.is_this_page
        #self.wait.until(presence_of_element_located((By.XPATH, "//input[@id='q']")))
        #hp.search_field.clear()
        #hp.search_field.send_keys(Keys.RETURN)
        #self.wait.until(presence_of_element_located((By.XPATH, "//div[@id='results']")))
        #element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "a[href^='./?go=movie'], div[id='results']>div[class='content']")))
        #if element.tag_name == 'div':
        #    return 0
        #else:
        #    return len(hp.movie_list)

    def is_film_found(self, film):
        self.search_film(film)
        element = self.wait.until(presence_of_element_located((By.XPATH, "//div[starts-with(@id, 'movie_')]")))
        return True

    def is_film_not_found(self, film):
        self.search_film(film)
        element = self.wait.until(presence_of_element_located((By.XPATH, "//div[text() = 'No movies where found.']")))
        return True

    def search_film(self, film):
        ip = self.internal_page
        ip.is_this_page
        ip.home_link.click()
        hp = self.home_page
        hp.is_this_page
        hp.search_field.clear()
        hp.search_field.send_keys(film.name)
        hp.search_field.send_keys(Keys.RETURN)
        hp.search_field.send_keys(Keys.RETURN)



